﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using UserSystemApi.Common.ResponseResults;
using UserSystemApi.Entity;
using UserSystemApi.Services.Account;
using UserSystemApi.Services.Account.Dto;

namespace UserSystemApi.Api.Controllers.Account
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class UserController : BaseController
    {
        public UserController(IAccountService accountService) : base(accountService)
        {
        }
        /// <summary>
        /// 获取所有的用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Users>> GetUserList()
        {
            var res = await _accountService.ListUserAll();
            return res;
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="userInputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<ResultHelp<string>>> AddUser(UserInputDto userInputDto)
        {
            int row = await _accountService.CreateAndUpdateUsers(userInputDto);
            if (row >= 1)
                return ResultHelp<string>.success("新增成功!");
            else
            {
                return ResultHelp<string>.error("新增失败!");
            }
        }
        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<Users>> GetUserById(int Id)
        {
            var res = await _accountService.GetUserById(Id);
            if (res == null)
            {
                HttpContext.Response.StatusCode = 214;
                return NotFound("没找到用户信息");
            }
            return res;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<ResultHelp<string>>> DeleteUser(int Id)
        {
            bool deleted = await _accountService.DeleteUserById(Id);
            if(deleted){
                return ResultHelp<string>.success("删除成功!");
            }
            else
            {
                return ResultHelp<string>.error("删除失败!");
            }
        }
    }
}
